package com.base.study.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.base.commonlib.utils.GlideUtil;
import com.base.commonlib.utils.SubStringUtil;
import com.base.study.R;
import com.base.study.bean.OnlineStudyListBean;

import java.util.List;

import me.zhouzhuo.zzhorizontalprogressbar.ZzHorizontalProgressBar;

/**
 * Created by zw on 2020/2/27.
 */

public class MyStudyRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public static final int TYPE_HEADER = 0; //Header
    public static final int TYPE_FOOTER = 1; //Footer
    public static final int TYPE_NORMAL = 2; //不带header和footer

    private List<OnlineStudyListBean> mData;
    private View mHeaderView;
    private View mFooterView;

    private Context mContext;

    public MyStudyRecyclerAdapter(Context context, List<OnlineStudyListBean> mData) {
        this.mData = mData;
        this.mContext = context;
    }

    public View getHeaderView() {
        return mHeaderView;
    }

    public void setHeaderView(View headerView) {
        mHeaderView = headerView;
        notifyItemInserted(0);
    }

    public View getFooterView() {
        return mFooterView;
    }

    public void setFooterView(View footerView) {
        mFooterView = footerView;
        notifyItemInserted(getItemCount() - 1);
    }

    @Override
    public int getItemViewType(int position) {
        if (mHeaderView == null && mFooterView == null) {
            return TYPE_NORMAL;
        }
        if (position == 0) {
            //第一个item应该加载Header
            return TYPE_HEADER;
        }
        if (position == getItemCount() - 1) {
            //最后一个,应该加载Footer
            return TYPE_FOOTER;
        }
        return TYPE_NORMAL;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mHeaderView != null && viewType == TYPE_HEADER) {
            return new ListHolder(mHeaderView);
        }
        if (mFooterView != null && viewType == TYPE_FOOTER) {
            return new ListHolder(mFooterView);
        }
        View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_my_curriculum, parent, false);
        return new ListHolder(layout);
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (getItemViewType(position) == TYPE_NORMAL) {
            if (holder instanceof ListHolder) {
                //这里加载数据的时候要注意，是从position-1开始，因为position==0已经被header占用了
                ((ListHolder) holder).tvName.setText(SubStringUtil.subStringCN(SubStringUtil.subStringCN(mData.get(position - 1).getName(), 30), 35));
                if (TextUtils.isEmpty(mData.get(position - 1).getCreateUserName())) {
                    ((ListHolder) holder).lin_my_author.setVisibility(View.GONE);
                } else {
                    ((ListHolder) holder).lin_my_author.setVisibility(View.VISIBLE);
                    ((ListHolder) holder).tvAuthor.setText(mData.get(position - 1).getCreateUserName() + "");
                }
                GlideUtil.load(mContext, mData.get(position - 1).getCover(), ((ListHolder) holder).imageView);
                //((ListHolder) holder).imageView.setText(mData.get(position-1).getName());

                ((ListHolder) holder).linearLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        //课程
                        if (mData.get(position - 1).getType() == 0) {
                            ARouter.getInstance().build("/curriculum/detail")
                                    .withString("id", mData.get(position - 1).getId())
                                    .withString("url", mData.get(position - 1).getCover())
                                    .navigation();
                        }
                        if (mData.get(position - 1).getType() == 1) {
                            //新闻
                            ARouter.getInstance().build("/home/homenew")
                                    .withString("title", mData.get(position - 1).getName())
                                    //.withString("detail", mData.get(position - 1).getDescription())
                                    .withString("id", mData.get(position - 1).getId())
                                    .withString("courseRootId",mData.get(position-1).getFirstCategory())
                                    .navigation();
                        }

                    }
                });
                ((ListHolder) holder).pb.setMax(100);
                try {
                    ((ListHolder) holder).pb.setProgress(mData.get(position - 1).getDuration());
                } catch (Exception e) {
                    ((ListHolder) holder).pb.setProgress(0);
                }

                return;
            }
            return;
        } else if (getItemViewType(position) == TYPE_HEADER) {

            return;
        } else {
            return;
        }
    }


    class ListHolder extends RecyclerView.ViewHolder {
        TextView tvName;
        TextView tvAuthor;
        ImageView imageView;
        LinearLayout linearLayout;
        LinearLayout lin_my_author;
        ZzHorizontalProgressBar pb;

        public ListHolder(View itemView) {
            super(itemView);
            //如果是headerview或者是footerview,直接返回
            if (itemView == mHeaderView) {
                return;
            }
            if (itemView == mFooterView) {
                return;
            }
            tvName = itemView.findViewById(R.id.my_cu_name);
            tvAuthor = itemView.findViewById(R.id.my_cu_author);
            imageView = itemView.findViewById(R.id.item_image);
            linearLayout = itemView.findViewById(R.id.lin_item);
            lin_my_author = itemView.findViewById(R.id.lin_my_author);
            pb = itemView.findViewById(R.id.pb);
            // tvName.setText(SubStringUtil.subStringCN(mData.get(po).getName(),35));
            // pb.setMax(100);
            //pb.setProgress(30);
            // tvAuthor.setText(bean.getIsCharge()+"");
            // tvNumber.setText(bean.getDuration()+"\t万人");

            //GlideUtil.load(mContext, bean.getCover(), imageView);
        }
    }

    //返回View中Item的个数，这个时候，总的个数应该是ListView中Item的个数加上HeaderView和FooterView
    @Override
    public int getItemCount() {
        if (mHeaderView == null && mFooterView == null) {
            return mData.size();
        } else if (mHeaderView == null && mFooterView != null) {
            return mData.size() + 1;
        } else if (mHeaderView != null && mFooterView == null) {
            return mData.size() + 1;
        } else {
            return mData.size() + 2;
        }
    }
}
